'Minijuego CRYSTAL CUT

CrystalCut()
STOP

#INCLUDE "VarComp.bas"
#INCLUDE once <putchars.bas>
#INCLUDE once <keys.bas>
#INCLUDE once <attr.bas>
#INCLUDE once <clearbox.bas>
#INCLUDE once <retrace.bas>
#INCLUDE "CC/cc.spr.bas"
#INCLUDE "CC/cuts_line.bas"
#INCLUDE "CC/SonidoCC.bas"
#INCLUDE once "utils/Doublesizeprint.bas"
#DEFINE MINY 8
#DEFINE MAXY 22
#DEFINE MINX 4
#DEFINE MAXX 14


Dim active(5, 7) As UBYTE => { _
{10,10,10,10,10,10,10,10}, _
{10,10,10,10,10,10,10,10}, _
{10,10,10,10,10,10,10,10}, _
{10,10,10,10,10,10,10,10}, _
{10,10,10,10,10,10,10,10}, _
{10,10,10,10,10,10,10,10}}

Dim initend(15,1) As UBYTE => {_
{12,14},_
{14,16},_
{14,18},_
{10,16},_
{22,12},_
{12,22},_ 'P5
{14,18},_
{18,14},_
{8,22},_
{16,16},_
{14,12},_
{8,16},_   'P11
{14,12},_
{8,18},_
{14,18},_
{22,14}_
}

Dim panels(15, 5, 7) As UBYTE => {_
{{2, 2, 1, 5, 3, 1, 0, 2},_  'P0 - EASY
{1, 0, 2, 0, 1, 2, 0, 1},_
{1, 1, 2, 2, 0, 0, 2, 0},_
{0, 2, 0, 1, 0, 2, 1, 0},_
{2, 1, 1, 2, 2, 1, 0, 2},_
{0, 1, 4, 0, 2, 0, 0, 1}},_
{{1, 0, 3, 2, 5, 1, 2, 0},_  'P1
{2, 0, 1, 0, 1, 2, 1, 0},_
{0, 1, 0, 2, 3, 0, 3, 2},_
{0, 2, 1, 0, 2, 1, 0, 1},_
{1, 0, 0, 2, 0, 2, 2, 0},_
{1, 2, 0, 4, 0, 1, 1, 0}},_
{{0, 1, 2, 3, 1, 5, 1, 2},_  'P2
{2, 0, 0, 1, 2, 3, 0, 0},_
{1, 0, 0, 2, 1, 0, 0, 1},_
{0, 2, 1, 0, 0, 2, 2, 0},_
{0, 1, 0, 0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2, 1, 0},_
{2, 1, 0, 0, 2, 0, 3, 3},_
{1, 3, 0, 2, 3, 1, 0, 1},_
{3, 2, 0, 2, 3, 3, 3, 3},_
{1, 3, 0, 0, 0, 3, 1, 4}}}

Dim relojcc As UINTEGER
Dim n As UBYTE
Dim ix,iy,endy As UBYTE
Dim x,y As UBYTE
Dim pix,piy As UBYTE
Dim col As UBYTE
Dim colstress As UBYTE
Dim stress As UBYTE
Dim maxstress As ubyte
Dim lightflag As ubyte
Dim t_old,t_new As UBYTE
Dim movimiento As UBYTE
Dim brklevel As UBYTE
Dim acuizq,acuder,acuarr As UBYTE
Dim colfondo As UBYTE
Dim movok As UBYTE
Dim panel As UBYTE
Dim colorsonarA(3) As UBYTE => {0,4,6,2}
Dim colorsonarB(3) As UBYTE => {0,7,1,3}
Dim colorsonar(3) As UBYTE

'Función principal
Sub CrystalCut()
    Dim i,j As UBYTE
    POKE (UInteger 23606, @spaceFnt-256)
    FLASH 0

    
    If ending=5
        CLS
        panel=paneltrn
    Else
        panel=panelgame
    End If
    
    ink 0: print at 0,0
    PRINT USR TRANSIT
    clearBox(0,2,30,16)
    
    over 0
    col=3
    col=panel+col
    col=col mod 7 + 1
    
    If col bAND %1
        colorsonar=colorsonarA
    Else
        colorsonar=colorsonarB
    End If
    
    brklevel=3
    stress=0
    lightflag=1
    movok=0
    movimiento=3
    
    endy=initend(panel,1)
    ix=4
    putChars(endy,ix,2,2,@cuts_line(8,0))
    x=0:
    y=endy/2-4
    active(x,y)=8
    
    ix=14
    iy=initend(panel,0)
    putChars(iy,ix,2,2,@cuts_line(0,0))
    pix=ix
    piy=iy
    x=pix/2-2
    y=piy/2-4
    dameValSuma()
    active(x,y)=0
    
    Dim calc As UBYTE
    putChars(0,2,2,2,@picoi(0))
    PaintData(0,2,2,2,@tiles_attr(4,0))
    putChars(0,16,2,2,@picoiinv(0))
    PaintData(0,16,2,2,@tiles_attr(4,0))
    For i=0 To 13
        calc=2+2*i
        putChars(calc,2,2,2,@crystal(0))
        PaintData(calc,2,2,2,@tiles_attr(4,0))
        putChars(calc,16,2,2,@crystalinv(0))
        PaintData(calc,16,2,2,@tiles_attr(4,0))
    Next i
    For i=0 To 5
        calc=4+2*i
        putChars(0,calc,2,2,@lati(0))
        PaintData(0,calc,2,2,@tiles_attr(4,0))
        putChars(30,calc,2,2,@latd(0))
        PaintData(30,calc,2,2,@tiles_attr(4,0))
    Next i
    putChars(30,2,2,2,@picod(0))
    PaintData(30,2,2,2,@tiles_attr(4,0))
    putChars(30,16,2,2,@picodinv(0))
    PaintData(30,16,2,2,@tiles_attr(4,0))
    
    paint(24,4,6,12,5)
    putChars(24,4,6,6,@cuts_panel1(0))
    putChars(24,10,6,6,@cuts_panel2(0))
    PRINT AT 2,8;"CRYSTAL CUT Exp";experience
    paint(8,2,8,1,65): paint(16,2,3,1,67)
    paint(20,2,4,1,66)
    ink 7: PRINT AT 6,25;"TIME"
    PRINT AT 11,24;"STRESS"
    
    print " "
    PRINT AT 17,10;"PANEL ";panel;" ";
    IF (panel<5)
        ink 5
        print "EASY"
    End If
    IF (panel>=5 AND panel<11)
        ink 6
        print "MED"
    End If
    IF (panel>=11 AND panel<16)
        ink 2
        print "HARD"
    End If
    
    paint(2,4,6,12,5)
    putChars(2,4,6,2,@cuts_panel3_arr(0))
    For i=6 To 13
        putChars(2,i,1,1,@cuts_filoizq(0))
        putChars(7,i,1,1,@cuts_filoder(0))
    Next i
    putChars(2,14,6,2,@cuts_panel3_ab(0))
    
    putChars(3,6,4,9,@cuts_cutter(0))
    PaintData(3,6,4,9,@cuts_cutter_Attr(0))
    
    colfondo=col<<3
    
    paint(8,4,16,12,colfondo)
    doubleSizePrint(12, 26, str(stress))
    paint(26,12,2,2,4)
    
    panels(panel,0,initend(panel,1)/2-4)=0
    
    relojcc=1499
    Dim offset As UBYTE
    Dim f1,f2 As ubyte
    Dim nota As UBYTE
    Dim veloc As ULONG
    Dim notaarr As UBYTE
    
    maxstress=9
    veloc=20
    snd128(1)
    offset=0
    ink col
    CCFail=1
    Do
        If relojcc mod (200-experience*20) = 0
            paper col
            sonar()
            over 1
            sonar()
            over 0
            paper 0: ink col
            snd128(1)
        End If
        
        If relojcc = 1000
            veloc=15
            brklevel=2
            showBroken()
        Else If relojcc = 500
            veloc=10
            brklevel=1
            showBroken()
        Else If relojcc = 200
            brklevel=0
            showBroken()
            veloc=5
        End If
        If relojcc mod veloc = 0
            showStress()
            notaarr=nota mod 50
            f1=Music_Not(notasA_neb4(notaarr),0)
            f2=Music_Not(notasA_neb4(notaarr),1)
            fxs(1,1)=f1:fxs(1,0)=f2
            f1=Music_Not(notasB_neb4(notaarr),0)
            f2=Music_Not(notasB_neb4(notaarr),1)
            fxs(1,3)=f1:fxs(1,2)=f2
            f1=Music_Not(notasC_neb4(notaarr),0)
            f2=Music_Not(notasC_neb4(notaarr),1)
            fxs(1,5)=f1:fxs(1,4)=f2
            waitretrace
            AYReg(0,fxs(1,0))
            AYReg(1,fxs(1,1))
            AYReg(2,fxs(1,2))
            AYReg(3,fxs(1,3))
            AYReg(4,fxs(1,4))
            AYReg(5,fxs(1,5))
            nota=nota+1
        End If
        
        If relojcc mod 5 = 0
            teclado()
        End If
        relojcc=relojcc-1
        
        If relojcc<1000
            offset=1
            print AT 7,25;0
            If relojcc<100
                offset=2
                print AT 7,25;"00"
                If relojcc<10
                    offset=3
                    print AT 7,25;"000"
                End If
            End If
        End If
        print AT 7,25+offset;relojcc
        
    Loop Until relojcc=0 Or stress>=maxstress Or CCFail=0
    SilenciarAY()
    
    If CCFail=0
        
        If ending=5
            paneltrn=paneltrn+1
            paneltrn=paneltrn mod 16
            
        Else
            panelgame=panelgame+1
            panelgame=panelgame mod 16
            
        End If
        If movimiento=1 And (t_old=3 Or t_old=9)
            t_new=6
        Else If movimiento=2 And (t_old=2 Or t_old=9)
            t_new=7
        Else If movimiento=3 And (t_old=6 Or t_old=1 Or t_old=7)
            t_new=1
        End If
        putChars(endy,4,2,2,@cuts_line(t_new,0))
        
        snd128(1)
        fan(4,0,0,1)
        fan(4,0,1,1)
        fan(4,1,0,1)
        fan(4,1,1,1)
        AYReg(8,0)
        AYReg(9,0)
        AYReg(10,0)
        ink 4: PRINT AT 17,10;"CRYSTAL CUTTED";
        
    Else
        CCFail=1
        ink 2: PRINT AT 17,10;"CRYSTAL BURST";
        sonar()
        snd128(2)
        
        For j=0 To 3
            For i=1 To 7
                paint(8,4,16,12,7-i)
                pause 4
            Next i
        Next j
        
        For i=1 To 7
            paint(8,4,16,12,i)
            pause 4
        Next i
        paint(8,4,16,12,col)
        
    End If
    SilenciarAY()
    pause 100
    
    If ending=5
        CLS
        If CCFail=1
            ending=0
            MODRun=MOD_MEN
        Else
            MODRun=MOD_CRY
        End If
    Else
        MODRun=MOD_JUE
    End If
END Sub

'Resalta los posibles movimientos en la posición actual
Sub showStress()
    Dim shine,light As UBYTE
    lightflag=1-lightflag
    x=pix/2-2
    y=piy/2-4
    shine=64*lightflag
    light=(col<<3)+shine
    If x<>0
        setAttr(pix-2,piy,light):setAttr(pix-1,piy,light):setAttr(pix-2,piy+1,light):setAttr(pix-1,piy+1,light)
    End If
    If y<>0 And (active(ix/2-2,(iy-2)/2-4)=10 Or active(ix/2-2,(iy-2)/2-4)=8)
        setAttr(pix,piy-2,light):setAttr(pix+1,piy-2,light):setAttr(pix,piy-1,light):setAttr(pix+1,piy-1,light)
    End If
    If y<>7 And (active(ix/2-2,(iy+2)/2-4)=10 Or active(ix/2-2,(iy+2)/2-4)=8)
        setAttr(pix,piy+2,light):setAttr(pix+1,piy+2,light):setAttr(pix,piy+3,light):setAttr(pix+1,piy+3,light)
    End If
    setAttr(12,3,12+shine)
    paint(4,6,3,3,16+shine)
    paint(6,7,1,2,24+shine)
    paint(4,6,1,2,24+shine)
END Sub

'Lectura de teclado
Sub teclado()
    Dim t As UBYTE
    Dim ix_old,iy_old As UBYTE
    
    If TipoControl=3
        n = IN(31)
    End If
    
    ix_old=pix
    iy_old=piy
    t_old=active(ix/2-2,iy/2-4)
    
    If GetKeyScanCode() <> 0 Or n>0
        If (TipoControl=3 And n bAND %10) Or Multikeys(Teclas(0))
            If iy>piy-2 And y<>0 And (active(ix/2-2,(iy-2)/2-4)=10 Or active(ix/2-2,(iy-2)/2-4)=8)
                If x<>0
                    over 1: print at ix-2,iy;acuarr: over 0
                End If
                If movimiento=3 And y<>7
                    over 1: print at ix,iy+2;acuder: over 0
                End If
                iy=iy-2:
                movimiento=1
                movok=1
            End If
        Elseif (TipoControl=3 And n bAND %1) Or Multikeys(Teclas(1))
            If iy<piy+2 And y<>7 And (active(ix/2-2,(iy+2)/2-4)=10 Or active(ix/2-2,(iy+2)/2-4)=8)
                If x<>0
                    over 1: print at ix-2,iy;acuarr: over 0
                End If
                If movimiento=3 And y<>0
                    over 1: print at ix,iy-2;acuizq: over 0
                End If
                iy=iy+2:
                movimiento=2
                movok=1
            End If
        Elseif (TipoControl=3 And n bAND %1000) Or Multikeys(Teclas(2))
            If ix>pix-2 And x<>0
                If y<>0
                    over 1: print at ix,iy-2;acuizq: over 0
                End If
                If y<>7
                    over 1: print at ix,iy+2;acuder: over 0
                End If
                If movimiento=2
                    over 1: print at ix,iy-2;acuizq: over 0
                Else If movimiento=1
                    over 1: print at ix,iy+2;acuder: over 0
                End If
                ix=ix-2
                movimiento=3
                movok=1
            End If
        End If
        If movok>0
            movok=0
            paint(8,4,16,12,colfondo)
            lightflag=1: showStress()
            pix=ix
            piy=iy
            x=ix/2-2
            y=iy/2-4
            t=active(x,y)
            If t_old=0
                If movimiento=1
                    t_old=3: t_new=5
                Else If movimiento=2
                    t_old=2: t_new=4
                Else If  movimiento=3
                    t_old=1: t_new=0
                End If
            Else If t_old=5
                If movimiento=1
                    t_old=9: t_new=5
                Else If movimiento=3
                    t_old=6: t_new=0
                End If
            Else If t_old=4
                If movimiento=2
                    t_old=9: t_new=4
                Else If movimiento=3
                    t_old=7: t_new=0
                End If
            End If
            
            putChars(iy_old,ix_old,2,2,@cuts_line(t_old,0))
            putChars(iy,ix,2,2,@cuts_line(t_new,0))
            
            
            If brklevel<3
                putCharsOverMode(iy_old,ix_old,2,2,3,@cuts_broken(brklevel,0))
                putCharsOverMode(iy,ix,2,2,3,@cuts_broken(brklevel,0))
            End If
            
            active(x,y)=t_new
            actualizaStress()
            dameValSuma()
            snd128(1)
        End If
    End If
    pause 5
end Sub

'Muestra la tensión total acumulada en el panel de la derecha
Sub actualizaStress()
    Dim stresslocal As UBYTE
    clearBox(26,12,2,2)
    colstress=4
    stresslocal=panels(panel,x,y)
    stress=stress+stresslocal
    ink 4:PRINT AT pix,piy;stresslocal:ink col
    If stress>9
        stress=9
    End If
    If stresslocal>0
        border 2
        snd128(2)
        border 0
        border 2
    Else
        border 4
        snd128(0)
        border 0
        border 4
    End If
    border 0
    doubleSizePrint(12,26,STR(stress))
    If stress>5
        colstress=2
    Else If stress>3
        colstress=3
    Else If stress>1
        colstress=6
    End If
    paint(26,12,2,2,colstress)
end Sub

'Muestra las tensiones totales en las 3 direcciones posibles (izq,der y arriba)
Sub dameValSuma()
    Dim i As UBYTE
    acuizq=0:acuder=0:acuarr=0
    If y<>0
        For i=0 To y-1
            acuizq=acuizq+panels(panel,x,i)
        Next i
        If movimiento<>2
            print at pix,piy-2;acuizq
        End If
    End If
    If y<>7
        For i=y+1 To 7
            acuder=acuder+panels(panel,x,i)
        Next i
        If movimiento<>1
            print at pix,piy+2;acuder
        End If
    End If
    If x<>0
        For i=0 To x-1
            acuarr=acuarr+panels(panel,i,y)
        Next i
        print at pix-2,piy;acuarr
    End If
    If x=0 And y=endy/2-4 And stress<maxstress
        CCFail=0
    End If
END Sub

'Muestra las tensiones del panel totales de forma temporal
Sub sonar()
    snd128(0)
    Dim i,j As ubyte
    For i=0 To 5
        For j=0 To 7
            If active(5-i,j)=10
                If ((j<>y-1 And j<>y+1 ) Or 5-i<>x)
                    If (j<>y Or 5-i<>x-1)
                        ink colorsonar(panels(panel,5-i,j))
                        print at 4+(5-i)*2,8+j*2;panels(panel,5-i,j)
                    End If
                    
                End If
            End If
        Next j
    Next i
END Sub

'Muestra los paneles con grietas
Sub showBroken()
    Dim i,j As ubyte
    For i=0 To 7
        For j=0 To 5
            If active(j,i)<>10
                putCharsOverMode(8+i*2,4+j*2,2,2,3,@cuts_broken(brklevel,0))
            End If
        Next j
    Next i
END Sub